"""
    自定义的分页组件，如果想要使用，要做以下几件事】

# 在视图函数中
def number_list(request):

    # 1.根据自己的情况筛选数据
    querySet = models.PhoneNumberInfo.objects.filter(**data_dict).order_by('level')

    # 2.实例化分页对象
    page_object = Pagination(request, querySet)

    context = {
        'querySet': page_object.page_querySet,      # 分完页的数据
        'page_string': page_object.html()           # 生成页码
    }
    return render(request, 'number_list.html',context)

# 在HTML中
 <ul class="pagination">

        {{ page_string }}

     </ul>

"""
from django.utils.safestring import mark_safe
from django.http.request import QueryDict
import copy


class Pagination(object):
    def __init__(self,request,querySet,page_size = 10,page_param='page',plus = 5):
        query_dict = copy.deepcopy(request.GET)
        query_dict._mutable = True
        self.query_dict=query_dict
        self.page_param = page_param
        page = request.GET.get(page_param,'1')
        if page.isdecimal():
            page = int(page)
        else:
            page=1
        self.page = page
        self.page_size = page_size
        self.start = (page - 1) * page_size
        self.end = page * page_size

        self.page_querySet = querySet[self.start:self.end]

        # 数据总条数
        total_count = querySet.count()

        # 总页码
        total_page_count, div = divmod(total_count, page_size)
        if div:
            total_page_count += 1
        self.total_page_count = total_page_count

        self.plus = plus

    def html(self):
        # 计算出当前页的前五页 后五页
        plus = 5
        if self.total_page_count <= 2 * plus + 1:
            start_page = 1
            end_page = self.total_page_count
        else:
            if self.page <= plus:
                start_page = 1
                end_page = 2 * plus + 1
            else:
                if self.page + plus > self.total_page_count:
                    start_page = self.page - plus
                    end_page = self.total_page_count
                else:
                    start_page = self.page - plus
                    end_page = self.page + plus

        # 页码
        page_str_list = []


        # 上一页
        if self.page > 1:
            self.query_dict.setlist(self.page_param,[self.page-1])
            prev = '<li><a href="?{}">上一页</a></li>'.format(self.query_dict.urlencode())
        else:
            self.query_dict.setlist(self.page_param, [1])
            prev = '<li><a href="?{}">上一页</a></li>'.format(self.query_dict.urlencode())

        page_str_list.append(prev)

        for i in range(start_page, end_page + 1):
            self.query_dict.setlist(self.page_param, [i])
            if i == self.page:
                ele = '<li class="active"><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
            else:
                ele = '<li><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
            page_str_list.append(ele)

        # 下一页
        if self.page < self.total_page_count:
            self.query_dict.setlist(self.page_param, [self.page+1])
            prev = '<li><a href="?{}">下一页</a></li>'.format(self.query_dict.urlencode())
        else:
            self.query_dict.setlist(self.page_param, [self.total_page_count])
            prev = '<li><a href="?{}">下一页</a></li>'.format(self.query_dict.urlencode())

        page_str_list.append(prev)
        page_string = mark_safe(''.join(page_str_list))

        return page_string